package com.lekang.le.controller;

import com.lekang.common.annotation.Anonymous;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.le.domain.PendingTasks;
import com.lekang.le.service.impl.AdmissionProcessServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admission/process")
public class AdmissionProcessController {

    @Autowired
    private AdmissionProcessServiceImpl admissionProcessService;

    /**
     * 启动入住申请流程
     */
    @PostMapping("/start")
    public AjaxResult startAdmissionProcess(@RequestBody Map<String, Object> admissionData) {
        try {
            int result = admissionProcessService.startAdmissionProcess(admissionData);
            return AjaxResult.success("入住申请提交成功", result);
        } catch (Exception e) {
            return AjaxResult.error("提交失败: " + e.getMessage());
        }
    }

    /**
     * 完成评估步骤
     */
    @PostMapping("/complete-assessment")
    public AjaxResult completeAssessment(@RequestBody Map<String, Object> params) {
        try {
            Long elderlyId = Long.valueOf(params.get("elderlyId").toString());
            Map<String, Object> assessmentData = (Map<String, Object>) params.get("assessmentData");
            
            int result = admissionProcessService.completeAssessmentStep(elderlyId, assessmentData);
            return AjaxResult.success("评估完成", result);
        } catch (Exception e) {
            return AjaxResult.error("评估失败: " + e.getMessage());
        }
    }

    /**
     * 完成审批步骤
     */
    @PostMapping("/complete-approval")
    public AjaxResult completeApproval(@RequestBody Map<String, Object> params) {
        try {
            Long elderlyId = Long.valueOf(params.get("elderlyId").toString());
            String action = (String) params.get("action");
            String comments = (String) params.get("comments");
            
            int result = admissionProcessService.completeApprovalStep(elderlyId, action, comments);
            return AjaxResult.success("审批完成", result);
        } catch (Exception e) {
            return AjaxResult.error("审批失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户待办任务
     */
    @GetMapping("/pending-tasks")
    public AjaxResult getPendingTasks() {
        try {
            List<PendingTasks> tasks = admissionProcessService.getUserPendingTasks();
            return AjaxResult.success(tasks);
        } catch (Exception e) {
            return AjaxResult.error("获取待办任务失败: " + e.getMessage());
        }
    }

    /**
     * 获取任务跳转信息
     */
    @GetMapping("/redirect/{taskId}")
    public AjaxResult redirectToTask(@PathVariable String taskId) {
        try {
            Map<String, Object> redirectInfo = admissionProcessService.getTaskRedirectInfo(taskId);
            return AjaxResult.success("跳转信息获取成功", redirectInfo);
        } catch (Exception e) {
            return AjaxResult.error("跳转失败: " + e.getMessage());
        }
    }

    /**
     * 完成入住配置步骤
     */
    @PostMapping("/complete-configuration")
    public AjaxResult completeConfiguration(@RequestBody Map<String, Object> params) {
        try {
            Long elderlyId = Long.valueOf(params.get("elderlyId").toString());
            Map<String, Object> configData = (Map<String, Object>) params.get("configData");
            
            int result = admissionProcessService.completeConfigurationStep(elderlyId, configData);
            return AjaxResult.success("配置完成", result);
        } catch (Exception e) {
            return AjaxResult.error("配置失败: " + e.getMessage());
        }
    }

    /**
     * 完成签约办理步骤
     */
    @PostMapping("/complete-contract")
    public AjaxResult completeContract(@RequestBody Map<String, Object> params) {
        try {
            Long elderlyId = Long.valueOf(params.get("elderlyId").toString());
            Map<String, Object> contractData = (Map<String, Object>) params.get("contractData");
            
            int result = admissionProcessService.completeContractStep(elderlyId, contractData);
            return AjaxResult.success("签约完成", result);
        } catch (Exception e) {
            return AjaxResult.error("签约失败: " + e.getMessage());
        }
    }
}